import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/home/province_level/widgets/widgets.dart';
import 'package:leader_help/utils/component/list/section.dart';
import 'package:leader_help/utils/util.dart';

import 'index.dart';

class ProvinceLevelController extends GetxController
    with GetSingleTickerProviderStateMixin {
  ProvinceLevelController();

  final state = ProvinceLevelState();

  // tap
  void handleTabTap(int index) {
    state.isTapTitle = true;
    Future.delayed(const Duration(milliseconds: 800)).then((value) {
      state.isTapTitle = false;
    });
    if (state.tabIndex == index) {
      return;
    }
    state.tabIndex = index;
    update();
    _getHeaderHeight();
    if (index == 0) {
      state.scrollCtrl.jumpTo(state.headerHeight);
    } else {
      var _position = state.positions[index - 1] + state.headerHeight;
      state.scrollCtrl.jumpTo(_position);
    }
  }

  void getData() {
    var _items1 = [
      ProvinceDiffItemModel(name: '提质发展传统产业', isLack: false, isLack2: false),
      ProvinceDiffItemModel(name: '培育壮大新兴产业', isLack: false, isLack2: false),
      ProvinceDiffItemModel(name: '前瞻布局未来产业', isLack: false, isLack2: true),
      ProvinceDiffItemModel(name: '优化提升现代服务业', isLack: false, isLack2: false),
      ProvinceDiffItemModel(name: '建设国家创新高地', isLack: false, isLack2: true),
      ProvinceDiffItemModel(
          name: '加强区域创新体系建设加快突破关键核心技术 深入推进科技体制改革创新',
          isLack: true,
          isLack2: false),
      ProvinceDiffItemModel(
          name: '推动更加充分更高质量就业', isLack: false, isLack2: false),
      ProvinceDiffItemModel(name: '全面推进乡村振兴', isLack: false, isLack2: false),
      ProvinceDiffItemModel(name: '推进财税金融体制改革', isLack: false, isLack2: true),
      ProvinceDiffItemModel(name: '推动经济社会数字化转型', isLack: false, isLack2: false),
      ProvinceDiffItemModel(
          name: '打造强大市场枢纽 构建内陆开放新高地', isLack: false, isLack2: false),
      ProvinceDiffItemModel(
          name: '优化区域发展布局 推进区域协调发展', isLack: true, isLack2: false),
      ProvinceDiffItemModel(name: '加快县域经济高质量发展', isLack: false, isLack2: true),
      ProvinceDiffItemModel(
          name: '统筹推进新基建和传统基建 健全现代化基础设施体系', isLack: true, isLack2: false),
    ];
    var _items2 = [
      ProvinceDiffItemModel(name: '建设国家创新高地', isLack: false, isLack2: false),
      ProvinceDiffItemModel(name: '“万人助万企”', isLack: false, isLack2: true),
      ProvinceDiffItemModel(name: '打造现代产业体系', isLack: false, isLack2: true),
      ProvinceDiffItemModel(
          name: '创建武汉具有全国影响力的科技创新中心和湖北东湖综合性国家科学中心',
          isLack: true,
          isLack2: false),
    ];
    var _items3 = [
      ProvinceDiffItemModel(
          name: '资金|《湖北省中小微外贸企业融资业务“楚贸贷”实施方案（试行）》',
          isLack: true,
          isLack2: false),
      ProvinceDiffItemModel(
          name: '资金|《河南省深化企业投资项目承诺制改革实施方案》', isLack: false, isLack2: true),
      ProvinceDiffItemModel(
          name: '土地|《河南省促进企业发展关于土地供应的相关政策》', isLack: false, isLack2: false),
      ProvinceDiffItemModel(
          name: '技术|《湖北省关于促进技术创新的若干建议》', isLack: true, isLack2: false),
      ProvinceDiffItemModel(
          name: '劳动力|《湖北省关于提高全民职业技能的若干办法》', isLack: true, isLack2: false),
    ];
    var _items4 = [
      ProvinceDiffItemModel(
          name: '《省优化营商环境办法条例》', isLack: false, isLack2: false),
      ProvinceDiffItemModel(
          name: '《省人民政府办公厅关于印发破解营商环境顽瘴痼疾集中攻坚若干措施的通知》',
          isLack: true,
          isLack2: false),
      ProvinceDiffItemModel(
          name: '《省人民政府办公厅关于印发支持中小微企业降成本若干措施的通知》',
          isLack: true,
          isLack2: false),
      ProvinceDiffItemModel(
          name: '湖北省建立营商环境投诉平台  ', isLack: true, isLack2: false),
      ProvinceDiffItemModel(
          name: '《关于集中修改涉及优化营商环境省本级地方性法规的决定》', isLack: true, isLack2: false),
      ProvinceDiffItemModel(
          name: '《河南省外商投资企业投诉工作办法》', isLack: false, isLack2: true),
      ProvinceDiffItemModel(
          name: '《河南省落实全国深化“放管服”改革着力培育和激发市场主体活力电视电话会议重点任务措施》',
          isLack: false,
          isLack2: true),
    ];

    if (state.isShowDiff) {
      _items1 = _items1
          .where((element) => element.isLack != element.isLack2)
          .toList();
      _items2 = _items2
          .where((element) => element.isLack != element.isLack2)
          .toList();
      _items3 = _items3
          .where((element) => element.isLack != element.isLack2)
          .toList();
      _items4 = _items4
          .where((element) => element.isLack != element.isLack2)
          .toList();
    }

    state.list = [
      _getSecMod('关键任务', '任务项', _items1, 0),
      _getSecMod('该省重点', '任务项', _items2, 1),
      _getSecMod('全要素高配', '相关政策/措施', _items3, 2),
      _getSecMod('营商环境优化', '相关政策/措施', _items4, 3),
    ];
    double _lastPosition = 0;
    state.positions = state.tabTitles
        .asMap()
        .map((key, value) {
          var _length = 0;
          switch (key) {
            case 0:
              _length = _items1.length;
              break;
            case 1:
              _length = _items2.length;
              break;
            case 2:
              _length = _items3.length;
              break;
            case 3:
              _length = _items4.length;
              break;
            default:
              break;
          }
          var _h = 55.r * _length + 40.r + 40.r;
          _lastPosition += _h;
          return MapEntry(
            key,
            _lastPosition,
          );
        })
        .values
        .toList();
    update();
  }

  SectionModel _getSecMod(String title, String subTitle,
      List<ProvinceDiffItemModel> mods, int index) {
    return SectionModel(
        index: 0,
        sectionHeader: SectionTitle(
          title: title,
        ),
        itemBuilder: (ctx, index) {
          return ProvinceLevelCell(
            title: subTitle,
            mods: mods,
          );
        },
        items: [1]);
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.mod = Get.arguments['current'];
    state.levelMod = Get.arguments['level'];
    state.tabTitles = ['关键任务', '省的重点', '要素配置', '营商环境'];

    // state.itemLis.itemPositions.addListener(() {
    //   var _firstIndex = state.itemLis.itemPositions.value.first.index;
    //   state.tabIndex = (_firstIndex) ~/ 2;
    // });

    state.scrollCtrl.addListener(() {
      if (state.isTapTitle) {
        ///点击引起滑动 return
        return;
      }
      var _off = state.scrollCtrl.offset;
      var _index = 0;
      if (state.positions.isEmpty) {
        return;
      }
      _getHeaderHeight();
      if (_off < state.positions[0] + state.headerHeight) {
        _index = 0;
      } else if (_off <= state.positions[1] + state.headerHeight) {
        _index = 1;
      } else if (_off < state.positions[2] + state.headerHeight) {
        _index = 2;
      } else {
        _index = 3;
      }
      if (_off >= state.scrollCtrl.position.maxScrollExtent) {
        _index = 3;
      }
      if (state.tabIndex != _index) {
        state.tabIndex = _index;
        update();
      }
    });
  }

  void _getHeaderHeight() {
    if (state.headerHeight == 0) {
      var box = state.headerKey.currentContext!.findRenderObject() as RenderBox;
      state.headerHeight = box.size.height;
    }
  }

  void setupPositions() {}

  void actionCellItem(String title) {
    Get.toNamed(RouterPath.pdf_view, arguments: title);
  }

  void actionDiff() {
    state.isShowDiff = !state.isShowDiff;
    getData();
    _getHeaderHeight();
    state.scrollCtrl.jumpTo(state.headerHeight);
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    getData();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
